package com.block.datapush.form.item;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.form.item.util.YiXinUtil;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class YiXinPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getPushLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) {

            String queryUrl = StrUtil.format(formInfo.getQueryUrl(), formInfo.getApiKey());
            String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("data", mobileMd5);
            paramMap.put("accessToken", formInfo.getApiKey());

            log.info("【长沙宜鑫】撞库开始【queryUrl】{}【paramMap】{}", queryUrl,  JSON.toJSONString(paramMap));
            String response = HttpUtil.createGet(queryUrl)
                    .form(paramMap)
                    .execute().body();
            log.info("【长沙宜鑫】撞库结束【result】{}", response);

            JSONObject responseObject = JSON.parseObject(response);
            String result = responseObject.getString("result");

            if (StrUtil.equals(result, "1")) {
                return true;
            }
            return false;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject extObject) {

        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);

            String requestUrl = StrUtil.format(formInfo.getRequestUrl(), formInfo.getApiKey());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("channelId", formInfo.getApiKey());
            paramMap.put("phone", memberExtInfo.getMobile());
            paramMap.put("realName", memberExtInfo.getUsername());
            paramMap.put("idCardNo", memberExtInfo.getIdCard());

            paramMap.put("location", StrUtil.format("{}/{}", memberExtInfo.getProvince(), memberExtInfo.getCity()));
            paramMap.put("loanLimit", YiXinUtil.getLoanAmount(memberExtInfo.getLoanAmount()));
            paramMap.put("educationLevel", "大专");
            paramMap.put("useFor", "消费");
            paramMap.put("loanLongTime", "12个月");
            paramMap.put("zmScore", YiXinUtil.getZhiMa(memberExtInfo.getZhimaScore()));
//            paramMap.put("wldLimit", memberExtInfo.getReqIp());
            paramMap.put("jdbtLimit", YiXinUtil.getBaiTiao(memberExtInfo.getBaitiao()));
            paramMap.put("vocation", "上班族");
            paramMap.put("employmentTime", "3年以上");
            paramMap.put("payrollType", "银行转账");
            paramMap.put("creditSituation", YiXinUtil.getYuQi(memberExtInfo.getYuqi()));
//            paramMap.put("creditLimit", memberExtInfo.getReqIp());
            paramMap.put("socialInsurance", YiXinUtil.getSheBao(memberExtInfo.getSocialInsurance()));
            paramMap.put("accumulationFund", YiXinUtil.getGjj(memberExtInfo.getGjj()));
            paramMap.put("businessInsurance", YiXinUtil.getBaoDan(memberExtInfo.getInsurance()));
            paramMap.put("houseProperty", YiXinUtil.getHouse(memberExtInfo.getHouse()));
            paramMap.put("carProperty", YiXinUtil.getCar(memberExtInfo.getCar()));
            paramMap.put("monthlyIncome", "8000");

            log.info("【长沙宜鑫】导单开始【requestUrl】{}【paramMap】{}", requestUrl,  JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(requestUrl)
                    .form(paramMap)
                    .execute().body();
            log.info("【长沙宜鑫】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);

            boolean success = resultObject.getBoolean("success");
            Integer errorCode = resultObject.getInteger("errorCode");
            String msg = resultObject.getString("msg");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(String.valueOf(errorCode));
            pushResultDTO.setMessage(msg);
            pushResultDTO.setParamContent(JSON.toJSONString(paramMap));
            if (success){
                pushResultDTO.setSuccess(true);
                pushResultDTO.setPushResult(PushResultEnum.SUCCESS.getType());
            }else{
                pushResultDTO.setPushResult(PushResultEnum.FAILED.getType());
                pushResultDTO.setSuccess(false);
            }
            return pushResultDTO;

    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }
}
